package cc.aiee.core.kit;

import java.util.ArrayList;
import java.util.List;

import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.serializer.SerializerFeature;

public class KitJson {

	public static String toJSONString(Object object) {
		return JSONObject.toJSONString(object);
	}

	public static String toJSONString(Object object, boolean prettyFormat, boolean ignoreNull) {
		List<SerializerFeature> sf = new ArrayList<>(2);
		if (prettyFormat) {
			sf.add(SerializerFeature.PrettyFormat);
		}
		if (!ignoreNull) {
			sf.add(SerializerFeature.WriteMapNullValue);
		}
		if (sf.isEmpty()) {
			return JSONObject.toJSONString(object);
		}
		SerializerFeature[] sfs = sf.toArray(new SerializerFeature[sf.size()]);
		return JSONObject.toJSONString(object, sfs);
	}

	/**
	 * 
	 * @param object
	 * @param prettyFormat 格式化
	 * @param ignoreNull 忽略null值
	 * @param singleQuotes 单引号
	 * @param sortField 字段排序
	 * @return
	 * @author YangLong <br>
	 * 2020年11月18日
	 */
	public static String toJSONString(Object object, boolean prettyFormat, boolean ignoreNull, boolean singleQuotes,
			boolean sortField) {
		List<SerializerFeature> sf = new ArrayList<>(4);
		if (prettyFormat == true) {
			sf.add(SerializerFeature.PrettyFormat);
		}
		if (!ignoreNull) {
			sf.add(SerializerFeature.WriteMapNullValue);
		}
		if (singleQuotes == true) {
			sf.add(SerializerFeature.UseSingleQuotes);
		}
		if (sortField == true) {
			sf.add(SerializerFeature.SortField);
		}
		if (sf.isEmpty()) {
			return JSONObject.toJSONString(object);
		}
		SerializerFeature[] sfs = sf.toArray(new SerializerFeature[sf.size()]);
		return JSONObject.toJSONString(object, sfs);
	}

}
